package cn.jobstart.common;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-02-24 10:40
 */

@Slf4j

public class PythonExecutor {


    public static String ocr(String paddleocrPath, String imgPath) throws Exception {

        List<String> cmd = new ArrayList<String>(); // 存放命令行参数的数组
        cmd.add(paddleocrPath);

        cmd.add("--image_dir");
        cmd.add(imgPath);
        cmd.add("--use_angle_cls");
        cmd.add("true");
        cmd.add("--use_gpu");
        cmd.add("true");
        cmd.add("--det");
        cmd.add("false");
        cmd.add("--show_log");
        cmd.add("False");


        ProcessBuilder pb = new ProcessBuilder();
        // 执行命令行
        pb.command(cmd);
        // 通知进程生成器是否合并标准错误和标准输出,把进程错误保存起来。
        pb.redirectErrorStream(true);
        // 开始执行进程
        Process process = pb.start();

        if (process.isAlive()) {
            process.waitFor();
        }

        InputStream inputStream = process.getInputStream();

        BufferedReader input = new BufferedReader(new InputStreamReader(inputStream));

        /**
         * [2023/02/24 11:01:28] ppocr INFO: Users / sunke / Desktop / python / ocr / 172. jpg
         * [2023 / 02 / 24 11:01:28]ppocr INFO:('D2510', 0.7689328193664551)
         */


        return readStr("2", input);


    }


    private static String readStr(String fileName, BufferedReader reader) {

        List<String> resultList = read(fileName, reader);
        if (StringUtil.isNullOrEmpty(resultList)) {
            return "";
        }
        String result = resultList.get(resultList.size() - 1);
        String[] resultTemp = result.split("ppocr INFO:");
        if (StringUtil.isNullOrEmpty(resultTemp) || resultTemp.length <= 1) {
            return "";
        }
        //('D2510', 0.7689328193664551)
        try {
            String value=resultTemp[1].trim();
            return value.substring(2, value.indexOf("',"));
        } catch (Exception e) {

        }
        return "";


    }

    private static List<String> read(String fileName, BufferedReader reader) {
        List<String> resultList = SkList.getInstance();
        String res = "";
        while (true) {
            try {
                if (!((res = reader.readLine()) != null)) break;
            } catch (IOException e) {
                log.error("读取python文件 fileName=" + fileName + " 读取结果异常", e);
            }


            log.info("ocr res:" + res);

            resultList.add(res);
        }
        return resultList;
    }


    public static void main(String[] args) throws Exception {


        long time = System.currentTimeMillis();
        System.out.println(ocr("/Users/sunke/opt/anaconda3/bin/paddleocr", "/Users/sunke/Desktop/python/ocr/1.jpg"));
        System.out.println("time:" + (System.currentTimeMillis() - time));

    }


}
